from flask import Flask, request, jsonify
from werkzeug.middleware.dispatcher import DispatcherMiddleware
from prometheus_client import make_wsgi_app
from pyspark import SparkConf, SparkContext


app = Flask(__name__)

app.wsgi_app = DispatcherMiddleware(app.wsgi_app, {
    '/metrics': make_wsgi_app()
})


conf = SparkConf().setAppName("FlaskSparkApp").setMaster("local[*]")
sc = SparkContext(conf=conf)


@app.route('/')
def hello():
    return "Hello, World!"

@app.route('/wordcount', methods=['GET'])
def word_count():
    input_file = request.args.get('input_file')
    if not input_file:
        return "Please provide an input file", 400

    text_file = sc.textFile(input_file)
    words = text_file.flatMap(lambda line: line.split(" "))
    word_counts = words.map(lambda word: (word, 1)).reduceByKey(lambda a, b: a + b)
    result = word_counts.collect()
    # sc.stop()

    return jsonify(result)


if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8083)
